/*
 * @Author: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @Date: 2025-10-12 20:23:21
 * @LastEditors: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @LastEditTime: 2025-10-30 14:32:54
 * @FilePath: /GateServer/src/service/Userservice.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "Userservice.h"
#include <jsoncpp/json/value.h>
#include <memory>
#include "../util/util.h"
namespace Yizhi {

    Json::Value Result::toJsonstirng(){
          Json::Value root;
    root["success"] = success;
    if (!success) {
        root["error"] =err_code;
        if (!message.empty()) {
            root["message"] = message;
        }
    } else {
        root["uid"] = uid;
    }
    return root;
    }

    Userservice::Userservice(std::shared_ptr<Validator> validator)
    :m_validator(validator){

    }

    Result Userservice::registeruser(const User&user){

        if(!user.validate()){
            return {false,Ecode::JSONFLASE,0,"invild input user"};
        }

        auto validatorrequest=m_validator->validate(user);
        if(validatorrequest!=RegisterValidator::validateResult::SUCCESS){
            switch (validatorrequest) {
                case Yizhi::RegisterValidator::validateResult::PASSWORD_MISMATCH:{
                    return {false,Ecode::PASSERROR,0,"password mismatch"};
                }
                case Yizhi::RegisterValidator::validateResult::VARIFY_CODE_EXPIRED:{
                    return {false,Ecode::VARIFY_CODE_EXPIRED,0,"vaild code expired"};
                }

                case Yizhi::RegisterValidator::validateResult::VARIFY_CODE_INVALID:{
                    return {false,Ecode::VARIFY_CODE_INVAILD,0,"varify code invaild"};
                }

                case Yizhi::RegisterValidator::validateResult::USER_EXISTS:{
                    return {false,Ecode::USEREXISTS,0,"user exists"};
                }
                default:
                    return {false,Ecode::UNKOWNERROR,0,"unkown error"};
            }
        }

        else{
        int uid=MysqlMgr::GetInstance()->RegUser(user.getUsername(),user.getEmail(), user.getPassword());
        if(uid<=0){
            return {false,Ecode::USEREXISTS,0,"create user failed"};
        }
        return {true,Ecode::SUCCESS,uid,"reg user success"};


        }

        
       
        
    }

    Result Userservice::resetpsw(const User&user){
        if(!user.validatewithoutusername()){
            return {false,Ecode::INVLIDINPUT,0,"user info error"}; 
        }

        auto result=m_validator->validate(user);
        if(result!=Validator::validateResult::SUCCESS){
            switch (result) {
                case Yizhi::Validator::validateResult::USER_NOT_EXISTS:{
                    return {false,Ecode::USER_NOT_EXISTS,0,"user not exists"};
                }
                case Yizhi::RegisterValidator::validateResult::VARIFY_CODE_EXPIRED:{
                    return {false,Ecode::VARIFY_CODE_EXPIRED,0,"vaild code expired"};
                }

                case Yizhi::RegisterValidator::validateResult::VARIFY_CODE_INVALID:{
                    return {false,Ecode::VARIFY_CODE_INVAILD,0,"varify code invaild"};
                }
                default:
                return {false,Ecode::UNKOWNERROR,0,"unknow error"};
            }
        }
        auto res=MysqlMgr::GetInstance()->RsetPsw( user.getUsername(),user.getEmail(),user.getPassword());
        return {true,Ecode::SUCCESS,0,"reg user success"};
    }

    Result Userservice::login(User&user){
        if(!user.validatewithoutemail()){
            return {false,Ecode::INVLIDINPUT,0,"用户名或密码为空"};
            
        }
        auto res =MysqlMgr::GetInstance()->login(user);
        
        if(res){
            return {true,Ecode::SUCCESS,user.getUid(),"login successs"};
        }
        else{
            return {false,Ecode::USER_NOT_EXISTS,0,"用户名或密码为空"};
        }
    }

}